<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>地址选择</title>
		<meta name="viewport" content="initial-scale=1, maximum-scale=1">
		<link rel="shortcut icon" href="/favicon.ico">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<link rel="stylesheet" type="text/css" href="css/checkdemo.css" />
		<script src="js/address.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery-1.11.3.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			
			.set_height {
				width: 100%;
				background-color: whitesmoke;
				margin-top: 1rem;
			}
			
			.name_phone {
				width: 90%;
				float: left;
				margin-left: 5%;
			}
			
			.name_phone_left {
				float: left;
				padding-top: 0.5rem;
			}
			
			.name_phone_right {
				float: right;
				padding-top: 0.5rem;
			}
			
			.select_address {
				margin-top: 1rem;
				width: 90%;
				float: left;
				margin-left: 5%;
				padding-bottom: 1rem;
			}
			
			.selsec_address_default {
				float: left;
				width: 90%;
				margin-left: 5%;
				padding-bottom: 1rem;
			}
		</style>
		<script type="text/javascript">
            function getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg); //匹配目标参数
                if (r != null) return unescape(r[2]);
                return null; //返回参数值
            }
            var addressid;
            var onlyId;
            var awardId;
            $(function() {
                addressid = getUrlParam('addressId');
                onlyId = getUrlParam('id');
                awardId = getUrlParam('awardId');
				$.ajax({
					type: "get",
					url: http_open + "/foot/user/getAddressAll",
					async: true,
					success: function(req) {
						console.log(req);
						if (req.error_code == 2) {
							alert("获取地址失败，请重新扫码登录");
							return;
						}
                        if(req.error_code == 1){
                            return;
                        }
						var length = req.data.length;
						console.log(length);
						for (var i = 0; i < length; i++) {
							var adress_text = req.data[i].address;
							var id = req.data[i].id
							var item_text = $("<div class='set_height setHeight" + i + "'></div>");
							var item_name_phone = $("<div class='name_phone id"+i+"'><p class='name_phone_left'>" + req.data[i].name + "</p><p class='name_phone_right'>" + req.data[i].phone + "</p></div>")
							var check_address = $("<div  class='opt select_address id"+i+"'><label>" + adress_text + "</label></div>")
							if (req.data[i].status == 1) {
								var set_default = $("<div class='selsec_address_default'><div class='opt'><input class='magic-radio' type='radio' name='radio' id='r" + i + "' value='"+id+"' checked><label for='r" + i + "' style='font-size: 1rem;'>是否设置为默认收件地址</label></div></div>")
							} else {
								var set_default = $("<div class='selsec_address_default'><div class='opt'><input class='magic-radio' type='radio' name='radio' id='r" + i + "' value='"+id+"'><label for='r" + i + "' style='font-size: 1rem;'>是否设置为默认收件地址</label></div></div>")
							}
							var clean = $("<br style='clear:both;' />");
							$(".col").append(item_text);
							$(".setHeight" + i + "").append(item_name_phone);
							$(".setHeight" + i + "").append(check_address);
							$(".setHeight" + i + "").append(set_default);
							$(".setHeight" + i + "").append(clean);
							$("#r" + i + "").click(function() {
								$.ajax({
									type: "get",
									url: http_open + "/foot/user/setDefaultAddress?addressId=" + $(this).val(),
									async: true,
									success: function(req) {
										if (req.error_code == 1) {
											alert("设置失败,请重新扫码登录")
										} else {
											console.log(req);
											alert("设置成功");
										}
									},
									error: function() {
										alert("设置失败");
									}
								});
							});
							$(".id"+i+"").click(function () { 
								var id = $(this).parent("div").children(".selsec_address_default").children(".opt").children("input").val();
                                window.location.href = "order.html?id="+id+"&onlyId="+onlyId+"&awardId="+awardId;
							})
							
							
							
							
						}
					},
					error: function(req) {
						console.log(req);
					}
				});
				$("#addaddress").click(function() {
                    window.location.href = "address.html?onlyId="+onlyId+"&awardId="+awardId;
				});
			})
		</script>
	</head>

	<body>

		<div style="width: 100%;">
			<div style="width: 90%;margin: auto;margin-top: 15px;height: 2rem;">
				请添加地址:
			</div>
			<div id="addaddress" style="width: 90%;margin: auto;height: 3rem;border: 1px dashed gainsboro;">
				<div style="height: 2rem;width: 2rem;margin: auto;background: url(img/add.png) no-repeat center;background-size: 100% 100%;margin-top: 0.5rem;"></div>
			</div>
			<div style="width: 90%;margin: auto;height: 1rem;margin-top: 15px;">
				<p>请选择地址:</p>
			</div>
			<div style="width:100%;">
				<div class="col"></div>
			</div>

		</div>
	</body>

</html>